home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / appletalk / netatalk / afs / afskrbsrc.sit.hqx / AFS Kerberos 1.0B0 / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-23  |  13.7 KB  |  610 lines

  1. /*
  2.  * Log application (for re-authenticating to the AFP server under AFS)
  3.  */
  4. /*
  5.  * Copyright (c) 1990 Regents of The University of Michigan.
  6.  * All Rights Reserved.
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software
  9.  * and its documentation for any purpose and without fee is hereby
  10.  * granted, provided that the above copyright notice appears in all
  11.  * copies and that both that copyright notice and this permission
  12.  * notice appear in supporting documentation, and that the name of
  13.  * The University of Michigan not be used in advertising or
  14.  * publicity pertaining to distribution of the software without
  15.  * specific, written prior permission. This software is supplied as
  16.  * is without expressed or implied warranties of any kind.
  17.  *
  18.  *    ITD Research Systems
  19.  *    University of Michigan
  20.  *    535 W. William Street
  21.  *    Ann Arbor, Michigan
  22.  *    +1-313-936-2652
  23.  *    netatalk@terminator.cc.umich.edu
  24.  */
  25. #include    <types.h>
  26. #include    <errors.h>
  27. #include    <resources.h>
  28. #include    <quickdraw.h>
  29. #include    <fonts.h>
  30. #include    <events.h>
  31. #include    <windows.h>
  32. #include    <menus.h>
  33. #include    <textedit.h>
  34. #include    <dialogs.h>
  35. #include    <memory.h>
  36. #include    <osutils.h>
  37. #include    <osevents.h>
  38. #include    <segload.h>
  39. #include    <strings.h>
  40. #include    <fcntl.h>
  41. #include    <files.h>
  42. #include    <appletalk.h>
  43. #include    <toolutils.h>
  44. #include    <traps.h>
  45. #include    <desk.h>
  46. #include    <devices.h>
  47. #include    <lists.h>
  48. #include    <traps.h>
  49. #include    <cursorctl.h>
  50.  
  51. #include    <conf.h>
  52. #include    <des.h>
  53. #include    <krb.h>
  54. #include    "uam.h"
  55. #include    "log.h"
  56. #include    "rsrc.h"
  57.  
  58. short             doDRVROpen( CntrlParam *ctlPB, DCtlPtr dCtl );
  59. pascal Boolean        selectfilter();
  60. short            findsessions( struct sesslist *sl, short resid );
  61. short            selectdialog( struct sesslist *sl, int n, short resid );
  62. pascal void        drawlist( DialogPtr dlog, short item );
  63. void            setname( struct sesslist *slp, Str255 vol, short width );
  64.  
  65. extern char    afp_server[];
  66.  
  67. pascal short
  68. DRVROpen( ctlPB, dCtl )
  69.     CntrlParam *ctlPB;
  70.     DCtlPtr dCtl;
  71. {
  72.     short        rc;
  73.     Ptr        mem;
  74.     long        old_A5, new_A5;
  75.     
  76.     /*
  77.      * initialize area off A5 for globals
  78.      */
  79.     if (( mem = NewPtr( A5Size())) == NULL ) {
  80.         return( -1 );
  81.     }
  82.     mysetup_A5( mem, &new_A5 );
  83.     old_A5 = SetA5( new_A5 );
  84.     mysetup_QD( old_A5 );
  85.     
  86.     /*
  87.      * open our DA (and do all the work!)
  88.      */
  89.     rc = doDRVROpen( ctlPB, dCtl );
  90.     
  91.     /*
  92.      * clean up A5 area
  93.      */
  94.     myrestore_A5( old_A5 );
  95.     DisposPtr( mem );
  96.     return( rc );
  97. }
  98.  
  99.  
  100. short
  101. doDRVROpen( ctlPB, dCtl )
  102.     CntrlParam *ctlPB;
  103.     DCtlPtr dCtl;
  104. {
  105.     struct sesslist    *slist;
  106.     short        resid;
  107.     GrafPtr        oldport;
  108.     char        name[ MAXNAMELEN + 1 ], passwd[ MAXPASSWDLEN + 1 ], *buf;
  109.     AFPLoginPrm    alp;
  110.     short        nsessions, srefnum, rf, off;
  111.     Boolean        dosetpw;
  112.     int        sess;
  113.     Handle        iconhdl;
  114.     DialogPtr    dlog;
  115.     SysEnvRec    senv;
  116.     des_cblock    sesskey;
  117.     acurHandle    acur_hdl;
  118.     
  119.     /*
  120.      * ignore multiple opens....
  121.      */
  122.     if ( dCtl->dCtlWindow != NULL ) {
  123.         return( noErr );
  124.     }
  125.     
  126.     if (( slist = (struct sesslist *)NewPtr( MAX_SESSIONS * sizeof( struct sesslist ))) == NULL ) {
  127.         return( -1 );
  128.     }
  129.     
  130.     GetPort( &oldport );
  131.     resid = OWNEDRSRCID(dCtl->dCtlRefNum);
  132.     InitCursor();
  133.     
  134.     /*
  135.      * check ROM revision
  136.      */
  137.     SysEnvirons( 1, &senv );
  138.     if ( senv.machineType < 0) {
  139.         doalert( resid + ALRT_BADROM );
  140.         SetPort( oldport );
  141.         DisposPtr( (Ptr)slist );
  142.         HUnlock( (Handle)acur_hdl );
  143.         ReleaseResource( (Handle)acur_hdl );
  144.         return( -1 );
  145.     }
  146.     if (( rf = HOpenResFile( senv.sysVRefNum, 0,
  147.             "\p:AppleShare Folder:AFS Kerberos", fsRdPerm )) < 0 ) {
  148.         doalert( resid + ALRT_FILEERR );
  149.         SetPort( oldport );
  150.         DisposPtr( (Ptr)slist );
  151.         HUnlock( (Handle)acur_hdl );
  152.         ReleaseResource( (Handle)acur_hdl );
  153.         return( -1 );
  154.     }
  155.     
  156.     nsessions = findsessions( slist, resid );
  157.     if ( nsessions <= 0 ) {
  158.         CloseResFile( rf );
  159.         SetPort( oldport );
  160.         DisposPtr( (Ptr)slist );
  161.         HUnlock( (Handle)acur_hdl );
  162.         ReleaseResource( (Handle)acur_hdl );
  163.         return( -1 );
  164.     }
  165.  
  166.     name[ 0 ] = 0;
  167.     
  168.     if (( acur_hdl = (acurHandle)Get1Resource( 'acur', ACUR_WATCH )) == NULL ) {
  169.         return( -1 );
  170.     }
  171.     HLock( (Handle)acur_hdl );
  172.     InitCursorCtl( acur_hdl );
  173.  
  174.     while( 1 ) {
  175.         sess = 0;
  176.         if ( nsessions > 1 && ( sess = selectdialog( slist, nsessions, resid )) < 0 ) {
  177.             break;
  178.         }
  179.         
  180.         srefnum = slist[ sess ].sess_refnum;
  181.         bcopy( slist[ sess ].sess_name + 1, afp_server + 1, slist[ sess ].sess_namelen );
  182.         afp_server[ 0 ] = slist[ sess ].sess_namelen;
  183.         ParamText( NULL, afp_server, NULL, NULL );
  184.         
  185.         bzero( (char*)&alp, sizeof( AFPLoginPrm ));
  186.         if ( OpenXPP( &alp.ioRefNum ) != noErr ) {
  187.             doalert( ALRT_LOCALERR );
  188.             break;
  189.         }
  190.         if ( get_server_status( alp.ioRefNum, slist[ sess ].sess_addr,
  191.                 &iconhdl, &dosetpw ) != 0 ) {
  192.             DisposPtr( (Ptr)slist );
  193.             HUnlock( (Handle)acur_hdl );
  194.             ReleaseResource( (Handle)acur_hdl );
  195.             return( -1 );
  196.         }
  197.         
  198.         /*
  199.          * perform the password dialog
  200.          */
  201.         dlog = passwddialog( name, passwd, iconhdl, resid + LOG_PASSWD_DLOG,
  202.                 false, srefnum, dosetpw );
  203.         if ( iconhdl != NULL ) {
  204.              DisposHandle( iconhdl );
  205.         }
  206.          
  207.         if ( dlog != NULL ) {    
  208.             alp.aspTimeout = 3;    /* kind of arbitrary! */
  209.             alp.ioCompletion = NULL;
  210.     
  211.             if ( dologin( false, true, &alp, name, passwd, &srefnum, sesskey ) == noErr ) {
  212.                 doalert( resid + ALRT_AUTHOK );
  213.             }
  214.             DisposDialog( dlog );
  215.         } else {
  216.              if ( nsessions == 1 ) {
  217.                 break;
  218.             }
  219.         }
  220.     }
  221.     CloseResFile( rf );
  222.     SetPort( oldport );
  223.     DisposPtr( (Ptr)slist );
  224.     HUnlock( (Handle)acur_hdl );
  225.     ReleaseResource( (Handle)acur_hdl );
  226.     return( -1 );
  227. }
  228.  
  229.  
  230.  
  231. pascal short
  232. DRVRPrime( ctlPB, dCtl )
  233.     CntrlParam    *ctlPB;
  234.     DCtlPtr        dCtl;
  235. {
  236.     
  237.     return( noErr );    /* Not used in this desk accessory */
  238. }
  239.  
  240. pascal short
  241. DRVRStatus( ctlPB, dCtl )
  242.     CntrlParam    *ctlPB;
  243.     DCtlPtr        dCtl;
  244. {
  245.     return( noErr );    /* Not used in this desk accessory */
  246. }
  247.     
  248.  
  249. pascal short
  250. DRVRControl( ctlPB, dCtl )
  251.     CntrlParam    *ctlPB;
  252.     DCtlPtr        dCtl;
  253. {
  254.     EventRecord        evt;
  255.     
  256.     extern    void    doCtlEvent();
  257.     extern    void    doPeriodic();
  258.  
  259.     switch (ctlPB->csCode) {
  260.         case accEvent:
  261. /*
  262.             HLock(dCtl->dCtlStorage);
  263.             doCtlEvent( *((EventRecord **) &ctlPB->csParam[0]),
  264.                     (Globals *)(*dCtl->dCtlStorage), dCtl->dCtlRefNum, dCtl->dCtlWindow );
  265.             HUnlock(dCtl->dCtlStorage);
  266. */
  267.             break;
  268.  
  269.         case accRun:        /* periodicEvent */
  270. /*
  271.             doPeriodic(dCtl);
  272. */
  273.             break;
  274.  
  275.         case accCursor:        /* null event to blink cursor */
  276. /*
  277.             evt.message = 0;
  278.             DialogSelect( &evt, NULL, NULL );
  279. */
  280.             break;
  281.             
  282.     }
  283.     return( noErr );
  284. }
  285.  
  286. #ifdef notdef
  287. void
  288. doCtlEvent(register EventRecord *theEvent, Globals *globals, short refnum, DialogPtr dlog)
  289. {
  290.     char        name[ 64 ], passwd[ 64 ];
  291.     short        item;
  292.     DialogPtr    adlog;
  293.     
  294. }
  295. #endif notdef
  296.  
  297. pascal short
  298. DRVRClose( ctlPB, dCtl)
  299.     CntrlParam    *ctlPB;
  300.     DCtlPtr        dCtl;
  301. {
  302.     return( noErr );
  303. }
  304.  
  305.  
  306. short
  307. findsessions( sl, resid )
  308.     struct sesslist        sl[];
  309.     short            resid;
  310. {
  311.     AVCB            *vcbp;
  312.     HParamBlockRec        hpb;
  313.     GetVolParmsInfoBuffer    gvpi;
  314.     ParamBlockRec        pb;
  315.     int            i, j;
  316.     short            listwidth, item, rc;
  317.     Rect            r;
  318.     long            bufsize;
  319.     unsigned char        uamused;
  320.     Ptr            vinfobuf;
  321.     VolumeType        voltype;
  322.     unsigned char        volname[ 28 ], *p, *q;
  323.     unsigned char        where[ 1 + 32 + 4 + 32 ];    /* room for "Server in Zone" */
  324.     short            sessrefnum;
  325.  
  326.     listwidth = LIST_RIGHT - LIST_LEFT - 17;
  327.     
  328.     /*
  329.      * form a list of remote sessions (by searching volume control blocks)
  330.      */
  331.     vcbp = (AVCB *)GetVCBQHdr()->qHead;
  332.     i = 0;
  333.     do {
  334.         bzero( (char *)&hpb, sizeof( hpb ));
  335.         hpb.ioParam.ioVRefNum = vcbp->vcbVRefNum;
  336.         hpb.ioParam.ioBuffer = (Ptr)&gvpi;
  337.         hpb.ioParam.ioReqCount = sizeof( gvpi );
  338. /* DebugStr( "\pabout to PBHGetVolParms" );    */
  339.         if ( PBHGetVolParms( &hpb, false ) != noErr ) {
  340.             continue;
  341.         }
  342.         
  343.         if ( ( 1 << bHasExtFSVol ) & gvpi.vMAttrib ) {        /* external volume */
  344. /*            DebugStr( "\pexternal volume" );    */
  345.             bzero( (char *)&pb, sizeof( pb ));
  346.             pb.ioParam.ioVRefNum = vcbp->vcbVRefNum;
  347.             pb.ioParam.ioBuffer = (Ptr)&bufsize;
  348. /* DebugStr( "\pabout to PBHGetVolMountInfoSize" );    */
  349.             if (( rc = PBGetVolMountInfoSize( &pb )) != noErr && rc != paramErr ) {
  350.                 continue;
  351.             }
  352.             
  353.             if ( rc == noErr ) {
  354.                 bufsize >>= 16;    /* size is in upper 16 bits? XXX */
  355.                 if (( vinfobuf = NewPtr( bufsize )) == NULL ) {
  356.                     doalert( resid + ALRT_NOMEMORY );
  357.                     return( -1 );
  358.                 }
  359.                 bzero( (char *)&pb, sizeof( pb ));
  360.                 pb.ioParam.ioVRefNum = vcbp->vcbVRefNum;
  361.                 pb.ioParam.ioBuffer = vinfobuf;
  362. /*    DebugStr( "\pabout to PBHGetVolMountInfo" );    */
  363.                 if (( rc = PBGetVolMountInfo( &pb )) != noErr ) {
  364.                     DisposPtr( vinfobuf );
  365.                     continue;
  366.                 }
  367.                 uamused = ((AFPVolMountInfo *)vinfobuf)->uamType;
  368.                 voltype = ((VolMountInfoHeader *)vinfobuf)->media;
  369.                 
  370.                 /*
  371.                  * copy volume name
  372.                  */
  373.                 p = (char *)vinfobuf + ((AFPVolMountInfo *)vinfobuf)->volNameOffset;
  374.                 bcopy( p, volname, *p + 1 );
  375.                 
  376.                 /*
  377.                  * create "Server in Zone" string
  378.                  */
  379.                 p = (char *)vinfobuf + ((AFPVolMountInfo *)vinfobuf)->serverNameOffset;
  380.                 q = where;
  381.                 bcopy( p, q, *p + 1 );
  382.                 q += ( *p + 1 );
  383.                 bcopy( " in ", q, 4 );
  384.                 q += 4;
  385.                 p = (char *)vinfobuf + ((AFPVolMountInfo *)vinfobuf)->serverNameOffset;
  386.                 bcopy( p, q, *p + 1 );
  387.                 DisposPtr( vinfobuf );
  388.             } else {
  389. /* DebugStr( "\pusing old kludgy check for AFS login" );    */
  390.                 /*
  391.                  * mount calls not supported; try using undocumented "old" method
  392.                  */            
  393.                 if ( GetPtrSize( (Ptr)vcbp ) != sizeof( AVCB )) {
  394.                     doalert( resid + ALRT_BADVERS );
  395.                     return( -1 );
  396.                 }
  397.                 uamused = vcbp->vcbLoginMethod;
  398.                 voltype = AppleShareMediaType;    /* assumed */
  399.                 bcopy( vcbp->vcbVN, volname, sizeof( volname ));
  400.                 bcopy( vcbp->vcbFindWhere + 14,    /* undocumented field XXX */
  401.                     where + 1, *vcbp->vcbFindWhere - 13 );
  402.                 where[ 0 ] = *vcbp->vcbFindWhere - 13;
  403.             }
  404.             
  405.             if ( voltype != AppleShareMediaType && uamused != UAM_LOGINCODE ) {
  406.                 continue;
  407.             }
  408.             sessrefnum = vcbp->vcbSRefNum;    /* undocumented field XXX */
  409.             
  410.             for ( j = 0; j < i; ++j ) {
  411.                 if ( sl[ j ].sess_refnum == sessrefnum ) {
  412.                     setname( &sl[ j ], volname, listwidth );
  413.                     break;
  414.                 }
  415.             }
  416.             if ( j == i ) {
  417.                 bzero( sl[ i ].sess_name, 256 );
  418.                 bcopy( where, sl[ i ].sess_name, 1 + where[ 0 ] );
  419.                 sl[ i ].sess_namelen = 1 + where[ 0 ];
  420.                 sl[ i ].sess_refnum = sessrefnum;
  421.                 bcopy( (char *)&gvpi.vMServerAdr, (char *)&sl[ i ].sess_addr, sizeof( long ));
  422.                 strcat( sl[ i ].sess_name + 1, " (" );
  423.                 sl[ i ].sess_name[ 0 ] += 2;
  424.                 sl[ i ].sess_namestate = 0;
  425.                 setname( &sl[ i ], volname, listwidth );
  426.                 ++i;
  427.             }
  428.         }
  429.     } while ( i < MAX_SESSIONS && ( vcbp = (AVCB *)vcbp->qLink ) != 0 );
  430.     
  431.     if ( i < 1 ) {
  432.         doalert( resid + ALRT_NOSERVERS );
  433.         return( -1 );
  434.     }
  435.     
  436.     return( i );
  437. }
  438.  
  439. void
  440. setname( slp, vol, width )
  441.     struct sesslist *slp;
  442.     Str255        vol;
  443.     short        width;
  444. {
  445.     char        *p;
  446.  
  447.     p = slp->sess_name + slp->sess_name[ 0 ];
  448.     
  449.     if ( StringWidth( slp->sess_name ) + StringWidth( vol ) +
  450.             StringWidth( "\p, ..." ) < width ) {
  451.         if ( slp->sess_namestate == 0 ) {
  452.             slp->sess_namestate = 1;
  453.             p++;
  454.         } else {
  455.             *p++ = ',';
  456.         }
  457.         strncpy( p, vol + 1, *vol );
  458.         p += *vol;
  459.         *p = ')';
  460.         slp->sess_name[ 0 ] += *vol + 1;
  461.     } else if ( slp->sess_namestate != 2 ) {
  462.         strncpy( p, ", ...)", 6 );
  463.         slp->sess_name[ 0 ] += 5;
  464.         slp->sess_namestate = 2;
  465.     }
  466. }
  467.  
  468. short
  469. selectdialog( sl, n, resid )
  470.     struct sesslist    sl[];
  471.     int        n;
  472.     short        resid;
  473. {
  474.     DialogPtr    dlog;
  475.     Handle        hdl;
  476.     Rect        r, bounds;
  477.     Point        pt;
  478.     short        item, c;
  479.     int        i;
  480.     EventRecord    event;
  481.     Boolean        ok_enabled, rc;
  482.     ListHandle    lh;
  483.      
  484.     placewindow( 'DLOG', resid + SELECT_DLOG );
  485.     if (( dlog = GetNewDialog( resid + SELECT_DLOG, (Ptr)NULL, (WindowPtr)-1 )) == NULL ) {
  486.         doalert( ALRT_LOCALERR );
  487.         return( -1 );
  488.     }
  489.     SetPort( dlog );
  490.     ok_enabled = true;
  491.     enablebutton( dlog, DLOG_OK, true, true );
  492.     GetDItem( dlog, DLOG_LIST, &item, &hdl, &r );
  493.     SetDItem( dlog, DLOG_LIST, userItem, (Handle)drawlist, &r );
  494.  
  495.     r.right -= 15;
  496.     SetRect( &bounds, 0, 0, 1, n );
  497.     SetPt( &pt, r.right - r.left, 16 );
  498.  
  499.     if (( lh = LNew( &r, &bounds, pt, 0, (WindowPtr)dlog, false, false, false, true )) == NULL ) {
  500.         return( -1 );
  501.     }
  502.     ((WindowPeek)dlog)->refCon = lh;
  503.     (*lh)->selFlags = lOnlyOne;
  504.     for ( i = 0; i < n; ++i ) {
  505.         SetPt( &pt, 0, i );
  506.         LSetCell( (Ptr)(sl[ i ].sess_name+1), sl[ i ].sess_name[ 0 ], pt, lh );
  507.     }
  508.     SetPt( &pt, 0 , 0 );
  509.     LSetSelect( true, pt, lh );
  510.     LDoDraw( true, lh );
  511.     
  512.     do {
  513.         ModalDialog( (ModalFilterProcPtr)selectfilter, &item );
  514.         SetPt( &pt, 0, 0 );
  515.         rc = LGetSelect( true, &pt, lh );        
  516.         if ( !rc && ok_enabled ) {
  517.             enablebutton( dlog, DLOG_OK, false, true );
  518.             ok_enabled = false;
  519.         } else if ( rc && !ok_enabled ) {
  520.             ok_enabled = true;
  521.             enablebutton( dlog, DLOG_OK, true, true );
  522.         }
  523.     } while ( item != DLOG_OK && item != DLOG_CANCEL );
  524.     
  525.     if ( item == DLOG_CANCEL ) {
  526.         LDispose( lh );
  527.         DisposDialog( dlog );
  528.         return( -1 );
  529.     }
  530.     SetPt( &pt, 0, 0 );
  531.     if ( !LGetSelect( true, &pt, lh )) {
  532.         pt.v = -1;
  533.     }
  534.     LDispose( lh );
  535.     DisposDialog( dlog );
  536.     return( pt.v );
  537. }
  538.  
  539. pascal void
  540. drawlist( dlog, item )
  541.     DialogPtr    dlog;
  542.     short        item;
  543. {
  544.     short        type;
  545.     Handle        hdl;
  546.     Rect        r;
  547.     ListHandle    lh;
  548.     
  549.     if ( item == DLOG_LIST ) {
  550.         GetDItem( dlog, item, &type, &hdl, &r );
  551.         InsetRect( &r, -1, -1 );
  552.         FrameRect( &r );
  553.         lh = (ListHandle) ((WindowPeek)dlog)->refCon;
  554.         HLock( (Handle)lh );
  555.         LUpdate( (*lh)->port->visRgn, lh );
  556.         HUnlock( (Handle)lh );
  557.     }
  558. }
  559.  
  560.  
  561. pascal Boolean
  562. selectfilter( dlog, event, item )
  563.     DialogPtr    dlog;
  564.     EventRecord    *event;
  565.     short        *item;
  566. {
  567.     char        c;
  568.     Point        pt;
  569.     short        type;
  570.     Rect        r;
  571.     Handle        hdl;
  572.     ListHandle    lh;
  573.     
  574.     lh = (ListHandle) ((WindowPeek)dlog)->refCon;
  575.  
  576.     switch (event->what ) {
  577.     case keyDown:
  578.     case autoKey:
  579.         c = event->message & charCodeMask;
  580.         if ( c == CHAR_ENTER || c == CHAR_RETURN ) {
  581.             GetDItem( dlog, DLOG_OK, &type, &hdl, &r );
  582.             *item = ( type & itemDisable ) ? 0 : DLOG_OK;
  583.             return( true );
  584.         }
  585.         if ( c == CHAR_ESC ) {
  586.             *item = DLOG_CANCEL;
  587.             return( true );
  588.         }
  589.         break;
  590.     case updateEvt:
  591.         if ( event->message == dlog ) {        /* for this dialog */
  592.             LUpdate( dlog->visRgn, lh );
  593.             return( false );
  594.         }
  595.         break;
  596.     case mouseDown:
  597.         GetDItem( dlog, DLOG_LIST, &type, &hdl, &r );
  598.         pt = event->where;
  599.         GlobalToLocal( &pt );
  600.         if ( PtInRect( pt, &r )) {
  601.             if ( LClick( pt, event->modifiers, lh )) {
  602.                 *item = DLOG_OK;
  603.             }
  604.             return( true );
  605.  
  606.         }
  607.         break;
  608.     }
  609.     return( false );
  610. }